﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WBFSe3.IO;
using WBFSe3.Wbfs;
using System.IO;
using WBFSe3.Interfaces;
using WBFSe3.Streams;

namespace WBFSe3.Formats
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public class WbfsIsoCreator : WbfsFormatCreator
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        Boolean isSparse;
        WbfsPath path;
        int lastUsed;
        int usedCount;
        int sectorSize;
        Stream stream;

        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public override WbfsPath Path { get { return this.path; } }
        public override int SectorSize { get { return this.sectorSize; } }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public override WbfsError Initialize(WbfsDriveInfo drive, String path, String filename,
            IWbfsGameInfo info, WbfsUsage usageTable, int sectorSize, int splitCount)
        {
            this.isSparse = drive.IsSparse;
            this.path = new WbfsPath(path, new String[] { filename });
            this.lastUsed = usageTable.GetLastOne();
            this.usedCount = usageTable.CountOnes();
            this.sectorSize = sectorSize;

            return WbfsError.Ok;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public override long EstimateFileSize()
        {
            if (this.isSparse) return (long)this.usedCount * this.sectorSize;
            else return (long)this.lastUsed * this.sectorSize;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public override WbfsError BeginCreate(out WbfsStream stream)
        {
            stream = new WbfsFileStream(this.path[0], this.sectorSize, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            this.stream = stream;
            return WbfsError.Ok;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public override WbfsError EndCreate(bool succeeded)
        {
            stream.Close();

            if (!succeeded)
                File.Delete(this.path[0]);

            return WbfsError.Ok;
        }
    }
}
